package com.corona.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.corona.Game;
import com.corona.game.util.TextureCache;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import org.lwjgl.system.windows.User32;

/* compiled from: Menu.java */
/* loaded from: pokewilds.jar:com/pkmngen/game/DrawUseTossMenu.class */
class DrawUseTossMenu extends Menu {
    Sprite arrow;
    Sprite textBox;
    public Action.Layer layer;
    Map<Integer, Vector2> getCoords;
    int curr;
    Vector2 newPos;
    Sprite helperSprite;
    int cursorDelay;
    String itemName;
    Menu prevMenu;
    ArrayList<String> words;
    Vector2 offset;

    public DrawUseTossMenu(Game game, Menu prevMenu, String itemName) {
        this(game, prevMenu, itemName, new Vector2());
    }

    public DrawUseTossMenu(Game game, Menu prevMenu, String itemName, Vector2 offset) {
        this.layer = Action.Layer.gui_105;
        this.getCoords = new HashMap();
        this.words = new ArrayList<>();
        this.prevMenu = prevMenu;
        this.itemName = itemName;
        this.offset = offset;
        this.cursorDelay = 0;
        this.arrow = new Sprite(TextureCache.get(Gdx.files.internal("battle/arrow_right1.png")), 0, 0, 5, 7);
        this.textBox = new Sprite(TextureCache.get(Gdx.files.internal("menu/usetoss_menu2.png")), 0, 0, 160, 144);
        this.getCoords.put(0, new Vector2(113.0f, 48.0f));
        this.getCoords.put(1, new Vector2(113.0f, 32.0f));
        this.newPos = new Vector2(113.0f, 48.0f);
        this.arrow.setPosition(this.newPos.x, this.newPos.y);
        this.curr = 0;
        this.words.add("USE");
        this.words.add("DROP");
    }

    @Override // com.corona.game.Action
    public String getCamera() {
        return "gui";
    }

    @Override // com.corona.game.Action
    public Action.Layer getLayer() {
        return this.layer;
    }

    @Override // com.corona.game.Action
    public void step(Game game) {
        if (this.prevMenu != null) {
            this.prevMenu.step(game);
        }
        game.uiBatch.draw(this.textBox, this.textBox.getX() + this.offset.x, this.textBox.getY() + this.offset.y);
        for (int i = 0; i < this.words.size(); i++) {
            String word = this.words.get(i);
            for (int j = 0; j < word.length(); j++) {
                game.uiBatch.draw(game.textDict.get(Character.valueOf(word.charAt(j))), ((float) (120 + (8 * j))) + this.offset.x, ((float) (48 - (16 * i))) + this.offset.y);
            }
        }
        if (!this.disabled) {
            if (InputProcessor.upJustPressed) {
                if (this.curr > 0) {
                    this.curr--;
                    this.newPos = this.getCoords.get(Integer.valueOf(this.curr));
                }
            } else if (InputProcessor.downJustPressed && this.curr < 1) {
                this.curr++;
                this.newPos = this.getCoords.get(Integer.valueOf(this.curr));
            }
            if (this.cursorDelay >= 2) {
                game.uiBatch.draw(this.arrow, this.newPos.x + this.offset.x, this.newPos.y + this.offset.y);
            } else {
                this.cursorDelay++;
            }
            if (InputProcessor.aJustPressed) {
                game.actionStack.remove(this);
                if (this.curr == 0) {
                    useItem(game, this.itemName);
                } else if (game.battle.drawAction == null) {
                    Vector2 pos = game.player.facingPos();
                    Tile currTile = game.map.tiles.get(pos);
                    if (currTile == null || currTile.isSolid || currTile.isSign()) {
                        this.disabled = true;
                        game.insertAction(new DisplayText(game, "Something is in the way!", (String) null, false, true, (Action) new SetField(this.prevMenu, "disabled", false, this.prevMenu)));
                        return;
                    }
                    this.disabled = true;
                    game.insertAction(new SelectAmount(this.itemName, pos, this));
                } else {
                    game.insertAction(this.prevMenu);
                    game.insertAction(new PlayMusic("error1", new SetField(this.prevMenu, "disabled", false, null)));
                }
            } else if (InputProcessor.bJustPressed) {
                this.prevMenu.disabled = false;
                game.actionStack.remove(this);
                game.insertAction(this.prevMenu);
            }
        }
    }

    public void useItem(Game game, String itemName) {
        String itemName2 = itemName.toLowerCase(Locale.ROOT);
        if (game.battle.drawAction == null) {
            ArrayList<String> notAllowedWhileFlying = new ArrayList<>();
            notAllowedWhileFlying.add("old rod");
            notAllowedWhileFlying.add("good rod");
            notAllowedWhileFlying.add("super rod");
            notAllowedWhileFlying.add("sleeping bag");
            notAllowedWhileFlying.add("escape rope");
            notAllowedWhileFlying.add("berry seed");
            notAllowedWhileFlying.add("manure");
            notAllowedWhileFlying.add("miracle seed");
            notAllowedWhileFlying.add("black apricorn");
            notAllowedWhileFlying.add("blue apricorn");
            notAllowedWhileFlying.add("green apricorn");
            notAllowedWhileFlying.add("pink apricorn");
            notAllowedWhileFlying.add("red apricorn");
            notAllowedWhileFlying.add("white apricorn");
            notAllowedWhileFlying.add("yellow apricorn");
            if (game.player.currFieldMove.equals("FLY") && notAllowedWhileFlying.contains(itemName2.toLowerCase(Locale.ROOT))) {
                game.insertAction(this.prevMenu);
                game.insertAction(new PlayMusic("error1", new SetField(this.prevMenu, "disabled", false, null)));
            } else if (itemName2.equals("sleeping bag")) {
                if (!game.player.currFieldMove.equals("")) {
                    this.disabled = true;
                    game.insertAction(this.prevMenu);
                    game.insertAction(new PlayMusic("error1", new SetField(this.prevMenu, "disabled", false, null)));
                    return;
                }
                Tile right = game.map.tiles.get(game.player.position.cpy().add(16.0f, 0.0f));
                if (right == null || right.isSolid || right.isLedge || right.is("door") || right.is("stairs")) {
                    this.disabled = true;
                    game.actionStack.remove(this);
                    game.insertAction(new DisplayText(game, "Not enough room!", (String) null, false, true, (Action) new SetField(this.prevMenu, "disabled", false, this.prevMenu)));
                    return;
                }
                Tile currTile = game.map.tiles.get(game.player.position.cpy().add(0.0f, 0.0f));
                if (currTile.routeBelongsTo != null && currTile.routeBelongsTo.isDungeon) {
                    this.disabled = true;
                    game.actionStack.remove(this);
                    game.insertAction(new DisplayText(game, "Canì use this in a dangerous area!", (String) null, false, true, (Action) new SetField(this.prevMenu, "disabled", false, this.prevMenu)));
                } else if (currTile.name.contains("stairs") || currTile.name.contains("mountain") || currTile.name.contains("snow") || currTile.name.contains("desert")) {
                    this.disabled = true;
                    game.actionStack.remove(this);
                    game.insertAction(new DisplayText(game, "Canì sleep on harsh terrain!", (String) null, false, true, (Action) new SetField(this.prevMenu, "disabled", false, this.prevMenu)));
                } else if (currTile.isGrass || game.player.nearAggroPokemon) {
                    this.disabled = true;
                    game.actionStack.remove(this);
                    game.insertAction(new DisplayText(game, "Canì use this while Pokémon are nearby!", (String) null, false, true, (Action) new SetField(this.prevMenu, "disabled", false, this.prevMenu)));
                } else {
                    if (game.type == Game.Type.CLIENT) {
                        game.client.sendTCP(new Network.Sleep(game.player.network.id, true));
                    }
                    game.player.spawnLoc = game.player.position.cpy();
                    game.player.spawnIndex = -1;
                    if (game.map.tiles != game.map.overworldTiles) {
                        game.player.spawnIndex = game.map.interiorTilesIndex;
                    }
                    game.playerCanMove = true;
                    game.player.acceptInput = false;
                    game.player.dirFacing = "right";
                    game.player.sleepingBagSprite.setPosition(game.player.position.x, game.player.position.y);
                    game.insertAction(new PlayerMoving(game, game.player, false, new SetField(game.player, "dirFacing", "left", new SetField(game.player, "currSprite", game.player.standingSprites.get("left"), new WaitFrames(game, 24, new SetField(game.player, "drawSleepingBag", true, new WaitFrames(game, 24, new PlayerMoving(game, game.player, true, new SetField(game.player, "isSleeping", true, null)))))))));
                }
            } else if (itemName2.equals("escape rope")) {
                Vector2 nearest = null;
                int dst2 = 0;
                Vector2[] positions = {new Vector2(-16.0f, 0.0f), new Vector2(0.0f, 16.0f), new Vector2(16.0f, 0.0f), new Vector2(0.0f, -16.0f)};
                Iterator<Vector2> it = game.map.edges.iterator();
                while (it.hasNext()) {
                    Vector2 pos = it.next();
                    Tile tile = game.map.overworldTiles.get(pos);
                    if (tile != null && tile.name.equals("sand3") && !tile.biome.contains("mountain") && !tile.isSolid) {
                        boolean found = false;
                        int length = positions.length;
                        int i = 0;
                        while (true) {
                            if (i >= length) {
                                break;
                            }
                            Tile otherTile = game.map.overworldTiles.get(pos.cpy().add(positions[i]));
                            if (otherTile != null && otherTile.name.equals("water2")) {
                                found = true;
                                break;
                            }
                            i++;
                        }
                        if (found) {
                            int dist = (int) game.player.position.dst2(pos);
                            if (nearest == null || (dist > 1024 && dist < dst2)) {
                                dst2 = dist;
                                nearest = pos;
                            }
                        }
                    }
                }
                if (nearest == null) {
                    this.disabled = true;
                    game.insertAction(this.prevMenu);
                    game.insertAction(new PlayMusic("error1", new SetField(this.prevMenu, "disabled", false, null)));
                    return;
                }
                Vector2 nearestFinal = nearest.cpy();
                game.playerCanMove = false;
                game.insertAction(new DisplayText(game, "Return to the shore?", (String) null, true, false, (Action) new DrawYesNoMenu(null, new DisplayText.Clear(game, new WaitFrames(game, 3, new FadeMusic(game.currMusic, -0.0125f, game.currMusic.getVolume(), new WaitFrames(game, 60, new SplitAction(new WaitFrames(game, 18, new RunCode(() -> {
                    game.cam.position.set(nearestFinal.x + 16.0f, nearestFinal.y, 0.0f);
                    game.player.position = nearestFinal.cpy();
                    if (game.player.hmPokemon != null) {
                        game.player.hmPokemon.dirFacing = "down";
                        game.player.hmPokemon.position = nearestFinal.cpy();
                        if (game.player.hmPokemon.standingAction instanceof Pokemon.Follow) {
                            ((Pokemon.Follow) game.player.hmPokemon.standingAction).onPlayer = true;
                        }
                    }
                    if (game.player.currFieldMove.equals("SURF")) {
                        game.player.swapSprites(game.player.hmPokemon);
                        game.player.hmPokemon.removeDrawActions(game);
                        game.player.currFieldMove = "";
                        game.player.hmPokemon = null;
                    }
                }, null)), new EscapeRope(new SetField(game.map, "currRoute", new Route("", 2), new SetField(game.map, "interiorTilesIndex", 100, new WaitFrames(game, 60, new SetField(game.musicController, "resumeOverworldMusic", true, new SetField(game, "playerCanMove", true, null))))))))))), new DisplayText.Clear(game, new WaitFrames(game, 3, new SetField(game, "playerCanMove", true, null))))));
            } else if (itemName2.contains("repel")) {
                game.player.setItemAmount(itemName2, game.player.getItemAmount(itemName2).intValue() - 1);
                if (game.player.getItemAmount(itemName2).intValue() <= 0) {
                    game.player.removeItem(itemName2);
                }
                game.player.repelCounter = 100;
                if (itemName2.contains("max")) {
                    game.player.repelCounter = User32.VK_PLAY;
                }
                game.insertAction(this.prevMenu);
                game.insertAction(new DisplayText(game, "Applied " + itemName2.toUpperCase(Locale.ROOT) + "!", "fanfare1.ogg", (Action) null, new SetField(this.prevMenu, "disabled", false, null)));
            } else if (itemName2.contains("rod")) {
                game.insertAction(this.prevMenu);
                game.player.currRod = itemName2;
                game.insertAction(new DisplayText(game, "Press Z to cast the line.", (String) null, false, true, (Action) new WaitFrames(game, 10, new RemoveAction(this.prevMenu, new SetField(game, "playerCanMove", true, null)))));
            } else if (itemName2.contains("apricorn") || itemName2.equals("manure") || itemName2.equals("miracle seed") || itemName2.equals("berry seed")) {
                game.insertAction(this.prevMenu);
                game.player.currPlanting = itemName2;
                if (game.player.hmPokemon != null && !game.player.currFieldMove.equals("")) {
                    game.player.swapSprites(game.player.hmPokemon);
                    game.player.hmPokemon.removeDrawActions(game);
                    game.player.hmPokemon = null;
                    game.player.currFieldMove = "";
                }
                String text = "Press Z to plant seeds.";
                if (itemName2.equals("manure")) {
                    text = "Press Z to fertilize saplings and small trees.";
                }
                game.insertAction(new DisplayText(game, text, (String) null, false, true, (Action) new WaitFrames(game, 10, new RemoveAction(this.prevMenu, new SetField(game, "playerCanMove", true, null)))));
            } else if (itemName2.equals("moomoo milk") || itemName2.equals("berry juice") || itemName2.equals("revive") || itemName2.equals("rare candy") || itemName2.contains(" berry") || Game.evoStones.contains(itemName2)) {
                this.disabled = true;
                game.insertAction(new DrawPokemonMenu.Intro(new DrawPokemonMenu(game, itemName2, this.prevMenu)));
            } else {
                game.insertAction(this.prevMenu);
                game.insertAction(new PlayMusic("error1", new SetField(this.prevMenu, "disabled", false, null)));
            }
        } else if (!itemName2.contains("ball") && !itemName2.contains("berry") && !itemName2.equals("moomoo milk") && !itemName2.equals("revive") && !itemName2.equals("poké doll") && !itemName2.equals("silph scope")) {
            game.insertAction(this.prevMenu);
            game.insertAction(new PlayMusic("error1", new SetField(this.prevMenu, "disabled", false, null)));
        } else if (itemName2.equals("silph scope") && !game.battle.oppPokemon.isGhost) {
            game.insertAction(this.prevMenu);
            game.insertAction(new PlayMusic("error1", new SetField(this.prevMenu, "disabled", false, null)));
        } else if (itemName2.contains("ball") && (game.battle.oppPokemon.isGhost || game.battle.oppPokemon.isEgg)) {
            game.insertAction(new DisplayText(game, game.battle.oppPokemon.nickname.toUpperCase(Locale.ROOT) + " canì be caught!", (String) null, (Action) null, new SetField(this.prevMenu, "disabled", false, this.prevMenu)));
        } else if (itemName2.equals("moomoo milk") || itemName2.equals("berry juice") || itemName2.contains(" berry") || itemName2.equals("revive")) {
            this.disabled = true;
            game.insertAction(new DrawPokemonMenu.Intro(new DrawPokemonMenu(game, itemName2, this.prevMenu)));
        } else {
            this.prevMenu.prevMenu.disabled = false;
            Action action = new SplitAction(new PlayMusic("click1", (Action) null), null);
            if (game.type == Game.Type.CLIENT) {
                action.append(new Battle.WaitTurnData(game, null));
                game.client.sendTCP(new Network.DoBattleAction(game.player.network.id, Battle.DoTurn.Type.ITEM, itemName2));
            } else {
                game.battle.network.turnData = new Network.BattleTurnData();
                game.battle.network.turnData.itemName = itemName2;
            }
            if (!itemName2.equals("silph scope")) {
                game.player.setItemAmount(itemName2, game.player.getItemAmount(itemName2).intValue() - 1);
                if (game.player.getItemAmount(itemName2).intValue() <= 0) {
                    game.player.removeItem(itemName2);
                }
            }
            action.append(new Battle.DoTurn(game, Battle.DoTurn.Type.ITEM, this.prevMenu.prevMenu));
            game.actionStack.remove(this);
            game.insertAction(action);
        }
    }

    /* compiled from: Menu.java */
    /* loaded from: pokewilds.jar:com/pkmngen/game/DrawUseTossMenu$DropItem.class */
    public static class DropItem extends Action {
        public Action.Layer layer = Action.Layer.gui_0;
        Vector2 pos;
        Tile currTile;
        String item;
        int amount;

        public DropItem(String item, int amount, Vector2 pos, Action nextAction) {
            super(new Object[0]);
            this.item = item;
            this.amount = amount;
            this.pos = pos;
            this.nextAction = nextAction;
        }

        @Override // com.corona.game.Action
        public String getCamera() {
            return "gui";
        }

        @Override // com.corona.game.Action
        public Action.Layer getLayer() {
            return this.layer;
        }

        @Override // com.corona.game.Action
        public void firstStep(Game game) {
            this.currTile = game.map.tiles.get(this.pos);
        }

        @Override // com.corona.game.Action
        public void step(Game game) {
            game.player.setItemAmount(this.item, game.player.getItemAmount(this.item).intValue() - this.amount);
            if (game.player.getItemAmount(this.item).intValue() <= 0) {
                game.player.removeItem(this.item);
            }
            Tile newTile = new Tile(this.currTile.name, "pokeball1", this.pos.cpy(), true, this.currTile.routeBelongsTo);
            newTile.hasItem = this.item;
            newTile.hasItemAmount = this.amount;
            if (game.type != Game.Type.CLIENT) {
                game.map.tiles.put(this.pos.cpy(), newTile);
            } else {
                game.client.sendTCP(new Network.TileData(newTile));
            }
            game.actionStack.remove(this);
            game.insertAction(this.nextAction);
        }
    }

    /* compiled from: Menu.java */
    /* loaded from: pokewilds.jar:com/pkmngen/game/DrawUseTossMenu$SelectAmount.class */
    static class SelectAmount extends Menu {
        String itemName;
        public static int amount = 0;
        Vector2 pos;
        public Action.Layer layer = Action.Layer.gui_105;
        int maxAmount = 1;
        Sprite textbox = new Sprite(TextureCache.get(Gdx.files.internal("amount_bg1.png")), 0, 0, 160, 144);

        public SelectAmount(String itemName, Vector2 pos, Menu prevMenu) {
            this.pos = pos;
            this.itemName = itemName;
            this.prevMenu = prevMenu;
        }

        @Override // com.corona.game.Action
        public String getCamera() {
            return "gui";
        }

        @Override // com.corona.game.Action
        public Action.Layer getLayer() {
            return this.layer;
        }

        @Override // com.corona.game.Action
        public void firstStep(Game game) {
            amount = 1;
        }

        @Override // com.corona.game.Action
        public void step(Game game) {
            if (this.prevMenu != null) {
                this.prevMenu.step(game);
            }
            this.textbox.draw(game.uiBatch);
            if (!this.disabled) {
                if (InputProcessor.upJustPressed) {
                    if (amount + 1 <= game.player.getItemAmount(this.itemName).intValue()) {
                        amount++;
                    }
                } else if (InputProcessor.downJustPressed) {
                    if (amount > 1) {
                        amount--;
                    }
                } else if (InputProcessor.rightJustPressed) {
                    amount += 10;
                    if (amount > game.player.getItemAmount(this.itemName).intValue()) {
                        amount = game.player.getItemAmount(this.itemName).intValue();
                    }
                } else if (InputProcessor.leftJustPressed) {
                    amount -= 10;
                    if (amount <= 1) {
                        amount = 1;
                    }
                }
                String word = "x" + String.format(Locale.ROOT, "%02d", Integer.valueOf(amount));
                for (int i = 0; i < word.length(); i++) {
                    game.uiBatch.draw(game.textDict.get(Character.valueOf(word.charAt(i))), (float) (130 + (8 * i)), 56.0f);
                }
                if (InputProcessor.aJustPressed) {
                    if (game.type == Game.Type.CLIENT) {
                        game.client.sendTCP(new Network.DropItem(game.player.network.id, this.itemName, amount, this.pos));
                    }
                    this.disabled = true;
                    game.actionStack.remove(this);
                    game.playerCanMove = false;
                    game.insertAction(new WaitFrames(game, 10, new SplitAction(new DropItem(this.itemName, amount, this.pos, null), new PlayMusic("seed1", new WaitFrames(game, 10, new SetField(game, "playerCanMove", true, null))))));
                } else if (InputProcessor.bJustPressed) {
                    game.insertAction(new PlayMusic("click1", (Action) null));
                    game.actionStack.remove(this);
                    this.prevMenu.disabled = false;
                    game.insertAction(this.prevMenu);
                }
            }
        }
    }
}
